<?php

namespace bc\model\wc;

use bc\ar\MActiveRecord;
use bc\model\UserModel;

class StaffModel extends MActiveRecord
{
	
	/**
	 * 定义数据表
	 */
	public static function tableName()
	{
		return '{{%wc_staff}}';
	}
	
	
	/**
	 * bc_user表实体名
	 */
	protected function getUserModelName()
	{
		return UserModel::getTableSchema()->name;
	}

	/**
	 * bc_hospital表实体名
	 */
	protected function getHospitalModelName()
	{
		return HospitalModel::tableName();
	}


	/**
	 * bc_bpoint表实体名
	 */
	protected function getBpointModelName()
	{
		return BpointModel::tableName();
	}
	/**
	 * 
	 * @param unknown $params
	 * @param string $page
	 * @param string $pageSize
	 * @param string $oderBy
	 */
	public function getStaffList($params, $page = null, $pageSize = null, $oderBy = null)
	{
		$ar = $this->find ()->select ( $this->columns )->leftJoin ( $this->getHospitalModelName(), self::getTableSchema ()->name . '.hospital_id=' . $this->getHospitalModelName() . '.id' )->leftJoin ( $this->getUserModelName(), self::getTableSchema ()->name . '.user_id=' . $this->getUserModelName() . '.id' );
		if ($params)
		{
			$ar->where ( $params );
		}
		if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset ( $offset )->limit ( $pageSize );
		}
		
		if ($this->sort != null)
			$ar->orderBy ( $oderBy );
			// $ar->groupBy ( 'id' );
		return $ar->asArray ()->all ();
	}
	
	/**
	 * 
	 * @param array $data
	 * @param string|array $condition
	 */
	public function batchUpdate($data, $condition)
	{
		return $this->updateAll($data, $condition);
	}

	/**
	 *
	 */
	public function getTotal($condition)
	{
		if(!empty($condition))
		{
			$ar = $this->find ()->select ( $this->columns )->leftJoin ( $this->getHospitalModelName(), self::getTableSchema ()->name . '.hospital_id=' . $this->getHospitalModelName() . '.id' )->leftJoin ( $this->getUserModelName(), self::getTableSchema ()->name . '.user_id=' . $this->getUserModelName() . '.id' );
			if ($condition)
			{
				$ar->where ( $condition );
			}
			return $ar->count();
		}
		else
			return 0;
	}

	/*根据采血点id获取采血点事务*/
	public function getStaffFromBid($bpointId)
	{
		if(!empty($bpointId))
		{
			$ar = $this->find ()->select ( $this->columns )->leftJoin ( $this->getUserModelName(), self::getTableSchema ()->name . '.user_id=' . $this->getUserModelName() . '.id' );
			if ($bpointId)
			{
				$ar->where ( $bpointId );
			}
			return $ar->asArray ()->all ();
		}
		else
			return 0;
	}
}